/**
 * 
 */
package com.hirstlogics.boxarrow.model;

import static org.junit.Assert.*;

import java.io.StringReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.junit.Test;

import com.hirstlogics.boxarrow.xml.IO;

/**
 * @author steve
 *
 */
public class DescriptionTest {

	private static final String ANOTHER_TEXT = "Another Text";
	private static final String SAMPLE_TEXT = "Sample Text";

	private Description sampleDescriptor() {
		Description d = new Description(SAMPLE_TEXT);
		return d;
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#addDesc(java.lang.String)}.
	 */
	@Test
	public void testAddDesc() {
		Description d = new Description("Zero");
		d.addDesc("One");
		d.addDesc("Two");
		d.addDesc("Three");
		System.out.println(d.toXML());
		assertTrue(d.toString().equals("ZeroOneTwoThree"));
		assertTrue(d.hasSubDesc());
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#clear()}.
	 */
	@Test
	public void testClear() {
		Description d = sampleDescriptor();
		d.clear();
		assertTrue(d.desc==null);
		assertTrue(d.subDesc==null);
		assertFalse(d.hasSubDesc());
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#Description(java.lang.String)}.
	 */
	@Test
	public void testDescription() {
		Description d = sampleDescriptor();
		assertTrue(d.toString().equals(SAMPLE_TEXT));
		assertFalse(d.hasSubDesc());
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#setDesc(java.lang.String)}.
	 */
	@Test
	public void testSetDesc() {
		Description d = new Description("");
		d.setDesc(ANOTHER_TEXT);
		assertTrue(ANOTHER_TEXT.equals(d.toString()));
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#toXML()}.
	 */
	@Test
	public void testToXML() {
		Description d = sampleDescriptor();
		String xmlRender = d.toXML();
		System.out.println(xmlRender);
		assertTrue(xmlRender.contains(SAMPLE_TEXT));
	    int divOpen = xmlRender.indexOf("<div>");
		int divText = xmlRender.indexOf(SAMPLE_TEXT);
		int divClos = xmlRender.indexOf("</div>");
		assertTrue((0<divOpen) && (divOpen<divText) && (divText<divClos));
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#toXML()}.
	 */
	@Test
	public void testReadXML() {
		String xmlText = "<ba:Description xmlns:ba=\"http://www.hirstlogics.com/boxarrow\">"
            +"<div>This is a first model<div>Sub</div><div>More<div>Yet</div></div><div>Rest</div></div>"
			+ "</ba:Description>";
		System.out.println("-----------");
		XMLInputFactory xif = XMLInputFactory.newInstance();
		StringReader input = new StringReader(xmlText);

		try {
			XMLStreamReader reader = xif.createXMLStreamReader(input);
			reader.next();
			Description d = IO.readDescription(reader);
			System.out.println(d);
			System.out.println(d.toXML());
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("-----------");
		

	}
	
}
